/*********************************************************************************
 *      Copyright:  (C) 2022 dairongan<2974932606@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  parse_mattini.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/29/2022)
 *         Author:  dairongan <2974932606@qq.com>
 *      ChangeLog:  1, Release initial version on "07/29/2022 10:12:51 AM"
 *                 
 ********************************************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include "iniparser.h"
#include "parse_mqttini.h"

#define INI_PATH  "/etc/mqtt.ini"

int parse_mqtt_ini(char *flag, s_mosquitto_data *point)
{
	dictionary         *ini = NULL;
    const  char        *str = NULL;

	if(!flag || !point)
	{
		printf("Invalid input arguments!\n");
		return -1;
	}
	
	ini = iniparser_load(INI_PATH);
	
	if(!ini)
	{
		printf("MQTT iniparser failure:%s\n", strerror(errno));
		return -1;
	}

	if( !strcmp(flag, "shiyanshi") )
	{
    	str = iniparser_getstring(ini, "shiyanshi:username", "NULL");
    	strncpy(point->user, str, strlen(str));

   	 	str = iniparser_getstring(ini, "shiyanshi:password", "NULL");
    	strncpy(point->password, str, strlen(str));
   
		str = iniparser_getstring(ini, "shiyanshi:hostname", "NULL"); 
		strncpy(point->hostname, str, strlen(str));

    	str = iniparser_getstring(ini, "shiyanshi:port", "NULL");
    	point->port = atoi(str);

    	str = iniparser_getstring(ini, "shiyanshi:pub_topic", "NULL");
    	strncpy(point->pub_topic, str, strlen(str));

		str = iniparser_getstring(ini, "shiyanshi:sub_topic", "NULL");
    	strncpy(point->sub_topic, str, strlen(str));

		str = iniparser_getstring(ini, "shiyanshi:client_id", "NULL");
    	strncpy(point->client_id, str, strlen(str));

    	str = iniparser_getstring(ini, "shiyanshi:KEEP ALIVE", "NULL");
    	point->keepalive = atoi(str);

		str = iniparser_getstring(ini, "shiyanshi:log_name", "NULL");
    	strncpy(point->log_name, str, strlen(str));

 		str = iniparser_getstring(ini, "shiyanshi:db_name", "NULL");
   		strncpy(point->db_name, str, strlen(str));

    	str = iniparser_getstring(ini, "shiyanshi:interval", "NULL");
    	point->interval = atoi(str);

		return 0;
	}
	
	else if( !strcmp(flag, "test") )
	{
		str = iniparser_getstring(ini, "test:username", "NULL");
    	strncpy(point->user, str, strlen(str));

   	 	str = iniparser_getstring(ini, "test:password", "NULL");
    	strncpy(point->password, str, strlen(str));
   
		str = iniparser_getstring(ini, "test:hostname", "NULL"); 
		strncpy(point->hostname, str, strlen(str));

    	str = iniparser_getstring(ini, "test:port", "NULL");
    	point->port = atoi(str);

    	str = iniparser_getstring(ini, "test:pub_topic", "NULL");
    	strncpy(point->pub_topic, str, strlen(str));

		str = iniparser_getstring(ini, "test:sub_topic", "NULL");
    	strncpy(point->sub_topic, str, strlen(str));

		str = iniparser_getstring(ini, "test:client_id", "NULL");
    	strncpy(point->client_id, str, strlen(str));

    	str = iniparser_getstring(ini, "test:KEEP ALIVE", "NULL");
    	point->keepalive = atoi(str);

		str = iniparser_getstring(ini, "test:log_name", "NULL");
    	strncpy(point->log_name, str, strlen(str));

		str = iniparser_getstring(ini, "test:db_name", "NULL");
    	strncpy(point->db_name, str, strlen(str));

    	str = iniparser_getstring(ini, "test:interval", "NULL");
    	point->interval = atoi(str);

		return 0;
	}

	else if( !strcmp(flag, "aliyun") )
	{
		return -1;
	}
	
	else if( !strcmp(flag, "tengxunyun") )
	{
		return -1;
	}
	
	else if( !strcmp(flag, "huaweiyun") )
	{
		return -1;
	}
	
	else
	{
		printf("Invalid input arguments!\n");
		return -1;
	}
}
